import matplotlib.pyplot as plt
import numpy as np

from BasisFunctions import BasisFuction


class Curve:
    def __init__(self) -> None:
        pass

    def fx(self, a):
        return (8 + np.sin(4 * np.pi * a)) * np.cos(np.pi * 2 * a) + 4

    def fy(self, a):
        return (8 + np.cos(4 * np.pi * a)) * np.sin(2 * np.pi * a) + 4

    # 定义参数η的范围（0到2π，确保闭合
    def estima(self, h):
        N = (2 * h + 1) * 2 + 10
        a = np.linspace(0, 1, N)

        sample_x = self.fx(a)
        sample_y = self.fy(a)

        C = np.vstack([sample_x[0], sample_y[0]])

        for i in range(1, N):
            C = np.vstack([C, sample_x[i], sample_y[i]])

        basis = BasisFuction(N, h, a)
        G = np.vstack([g for g in basis.construct()])
        Xi = np.linalg.inv(G.T @ G) @ G.T @ C
        return Xi
